//
//  MYRadioGroup.m
//  HeXun-zixuan
//
//  Created by 范名扬 on 16/2/22.
//  Copyright © 2016年 com.hztc. All rights reserved.
//

#import "MYRadioGroup.h"
#import "MYCustomLeftRightBtn.h"

#define kRadioGroupID 0


@interface MYRadioGroup ()
@property (nonatomic, strong)UIImageView         *pBottomImageView;
@property (nonatomic, strong)NSMutableArray      *pButtonArray;
@property (nonatomic, strong)NSMutableArray      *pLineArray;
@property (nonatomic, strong)NSMutableArray      *pUnderLineArray;
@property (nonatomic, assign)BOOL                 isHiddenUnderLineView;
@property (nonatomic, assign)CGFloat              nUnderLineW;

@property (nonatomic, assign)BOOL isResetW;
@end

@implementation MYRadioGroup

- (UIImageView *)pBottomImageView
{
    if (!_pBottomImageView) {
        _pBottomImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 0, self.frame.size.width, self.frame.size.height)];
        _pBottomImageView.userInteractionEnabled = YES;
    }
    return _pBottomImageView;
}
- (NSMutableArray *)pButtonArray
{
    if (!_pButtonArray) {
        _pButtonArray = [NSMutableArray arrayWithCapacity:10];
    }
    return _pButtonArray;
}
- (NSMutableArray *)pLineArray
{
    if (!_pLineArray) {
        _pLineArray = [NSMutableArray arrayWithCapacity:10];
    }
    return _pLineArray;
}
- (NSMutableArray *)pUnderLineArray
{
    if (!_pUnderLineArray) {
        _pUnderLineArray = [NSMutableArray arrayWithCapacity:10];
    }
    return _pUnderLineArray;
}

- (instancetype)initWithFrame:(CGRect)frame{
    self = [super initWithFrame:frame];
    if (self) {
        [self setOriginalViews];
    }
    return self;
}

- (void)setOriginalViews
{
    [self addSubview:self.pBottomImageView];
    self.backgroundColor = [UIColor whiteColor];
    
    _nUnderLineW = 40;
}

- (NSInteger)getButtonNum
{
    return _pButtonArray.count;
}

- (void)setRadioGroup:(NSArray *)radioGroup
{
    for (int i = 0; i < self.pButtonArray.count; i++) {
        UIView *btn = _pButtonArray[i];
        UIView *underLine = _pUnderLineArray[i];
        UIView *line = nil;
        if (i <= _pLineArray.count - 1) {
           line = _pLineArray[i];
        }
        [btn removeFromSuperview];
        [line removeFromSuperview];
        [underLine removeFromSuperview];
    }
    [self.pButtonArray removeAllObjects];
    [self.pLineArray removeAllObjects];
    [self.pUnderLineArray removeAllObjects];
    
    for (int i = 0; i < radioGroup.count; i++) {
        NSString *pTitle = radioGroup[i];
        UIButton *btn = nil;
        UIView *lineView = nil;
        if (i == radioGroup.count - 1) {
            btn = [[MYCustomLeftRightBtn alloc] init];
        }else{
            btn = [[UIButton alloc] init];
        }
        if (i < radioGroup.count - 1) {
            lineView = [[UIView alloc] init];
            lineView.backgroundColor = [UIColor lightGrayColor];
        }
        
        UIView *underLineView = [[UIView alloc] init];
        underLineView.hidden = YES;
        underLineView.backgroundColor = kREDColor;
        
        btn.tag = kRadioGroupID + i;
        btn.userInteractionEnabled = YES;
        [btn setTitle:pTitle forState:UIControlStateNormal];
        [btn setTitleColor:kMYCustomColor(85, 85, 85, 1) forState:UIControlStateNormal];
        [btn setTitleColor:kREDColor forState:UIControlStateSelected];
        [btn addTarget:self action:@selector(btnOnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.pBottomImageView addSubview:btn];
        [self.pBottomImageView addSubview:lineView];
        [self.pBottomImageView addSubview:underLineView];
        if (btn) {
            [_pButtonArray addObject:btn];
        }
        if (lineView) {
            [_pLineArray addObject:lineView];
        }
        [_pUnderLineArray addObject:underLineView];
    }
}

- (void)btnOnClick:(UIButton *)btn
{
    [self onClickSelectedBtn:btn];
    [self.parent upToParentWithObjc:self withType:kHZ_radioGroupClick_Action withParamter:btn];
}

- (void)onClickSelectedBtn:(UIButton *)btn
{//直接选中
    NSInteger nNum = _pButtonArray.count;
    if ((self.nRadioGroupType == KMSRadioGroupSepicalButtonType) && (btn.tag == kRadioGroupID + nNum - 1)) {
        btn.selected = NO;
        if ([btn.currentImage isEqual:[UIImage imageNamed:@"minute"]]) {
            [btn setImage:[UIImage imageNamed:@"minuteTop"] forState:UIControlStateNormal];
        }
    }else{
        for (UIButton *btn in _pButtonArray) {
            btn.selected = NO;
        }
        for (UIView *underLine in _pUnderLineArray) {
            underLine.hidden = YES;
        }
        btn.selected = YES;
    }
    if (btn.selected) {
        if (!self.isHiddenUnderLineView) {
            UIView *underLine = _pUnderLineArray[btn.tag - kRadioGroupID];
            underLine.hidden = NO;
        }
    }
}

- (void)onClickSelectedBtnFromOther:(UIButton *)btn
{//间接选中
    for (UIView *underLine in _pUnderLineArray) {
        underLine.hidden = YES;
    }
    for (UIButton *btn in _pButtonArray) {
        btn.selected = NO;
    }
    btn.selected = YES;
    if (btn.selected) {
        if (!self.isHiddenUnderLineView) {
            UIView *underLine = _pUnderLineArray[btn.tag - kRadioGroupID];
            underLine.hidden = NO;
        }
    }
}

- (void)setHiddenSeparateLine:(BOOL)ishiddenSeparateLine andHiddenUnderline:(BOOL)ishiddenUnderline
{
    if (ishiddenSeparateLine) {
        for (UIView *separateLine in _pLineArray) {
            separateLine.hidden = YES;
        }
    }
    
    self.isHiddenUnderLineView = ishiddenUnderline;
}

- (void)setImage:(NSString *)imageName withIndex:(NSInteger)index
{
    UIButton *btn = _pButtonArray[index];
    [btn setImage:[UIImage imageNamed:imageName] forState:UIControlStateNormal];
}

- (void)resetButtonSelectedBackImage:(NSString *)selectedBackImage andSelectedTitleColor:(UIColor *)selectedTitleColor andNormalTitleColor:(UIColor *)normalTitleColor
{
    for (UIButton *btn in _pButtonArray) {
        [btn setTitleColor:selectedTitleColor forState:UIControlStateSelected];
        [btn setTitleColor:normalTitleColor forState:UIControlStateNormal];
        [btn setBackgroundImage:[UIImage imageNamed:selectedBackImage] forState:UIControlStateSelected];
    }
}

- (void)resetAllButtonFont:(UIFont *)font
{
    for (UIButton *btn in _pButtonArray) {
        btn.titleLabel.font = font;
    }
}

- (void)resetUnderLineWidth:(CGFloat)underLineW isReset:(BOOL)isReset
{
    _isResetW = isReset;
    if (underLineW > 0) {
        _nUnderLineW = underLineW;
    }
    [self setNeedsLayout];
}

- (void)resetSelectedButtonWithTitle:(NSString *)title andIndex:(NSInteger)index
{
    UIButton *pSelectedBtn = _pButtonArray[index];
//    [pSelectedBtn setImage:nil forState:UIControlStateNormal];
    [pSelectedBtn setTitle:title forState:UIControlStateNormal];
}

- (void)setSelectedButton:(NSInteger)index
{
    NSInteger nCount = _pButtonArray.count;
    if (index > nCount - 1 || index < 0) {
        return;
    }
    UIButton *btn = _pButtonArray[index];
    [self onClickSelectedBtnFromOther:btn];
}

- (void)setSelectedButtonWithTitle:(NSString *)title
{
    NSInteger nIndex = 0;
    for (int i = 0; i < _pButtonArray.count; i++) {
        UIButton *btn = _pButtonArray[i];
        if ([[btn currentTitle] isEqualToString:title]) {
            nIndex = i;
            break;
        }
    }
    [self setSelectedButton:nIndex];
}

- (UIButton *)getSelectedButton
{
    UIButton *selectedBtn = nil;
    for (UIButton *btn in _pButtonArray) {
        if (btn.selected) {
            selectedBtn = btn;
        }
    }
    
    return selectedBtn;
}

- (void)layoutSubviews{
    NSInteger nCount = _pButtonArray.count;
    CGFloat nRadioW = self.frame.size.width/nCount;
    
    for (int i = 0; i < nCount; i++) {
        UIButton *btn = _pButtonArray[i];
        UIView *lineView = nil;
        if (i < nCount - 1) {
            lineView = _pLineArray[i];
        }
        btn.frame = CGRectMake(i*nRadioW, 0, nRadioW, self.frame.size.height);
        lineView.frame = CGRectMake(CGRectGetMaxX(btn.frame) - 0.5, 0, 0.5, btn.frame.size.height);
        
        UIView *underLineView = _pUnderLineArray[i];
        CGFloat underLineViewX = nRadioW/2 + nRadioW*i;
        if (!_isResetW) {
            _nUnderLineW = nRadioW - 2;
        }
        underLineView.center = CGPointMake(underLineViewX, self.frame.size.height - 1);
        underLineView.bounds = CGRectMake(0, 0, _nUnderLineW, 2);
    }
    _pBottomImageView.frame = CGRectMake(0, 0, self.frame.size.width, self.frame.size.height);
}


@end
